package com.ruoyi.web.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.util.EncodingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 *
 * </pre>
 *
 * @author zhangli
 * @since 2023/11/13
 */
@Slf4j
@Component
public class HttpUtil {

    @Data
    public static  class IpAddressDto{

        private String region;

        private String city;

        private String country;

        private String ip;

        private String isp;
    }

    @Autowired
    private HttpServletRequest request;


    @Autowired
    private HttpClient httpClient;



    public HttpUtil(){

    }

    public String getIp() {
        return getIp(request);
    }

    public static String getIp(HttpServletRequest request) {
        String ipAddress = request.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
        }
        return ipAddress;
    }



    public IpAddressDto getCity(String ip){
        String host = "https://ipaddquery.market.alicloudapi.com/ip/address-query";
        String appcode = "4148cff953894a3b8efc3fe8d972bd3d";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("ip", ip);

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            String data = httpClient.doPostForm(host,  bodys,  headers);
            data = EncodingUtil.getString(data.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8.toString());
            log.info("ip: {}", data);
            JSONObject result = (JSONObject) JSONObject.parse(data);
            if(result.getInteger("code") == 200) {
                IpAddressDto ipAddressDto = JSON.parseObject(result.getString("data"), IpAddressDto.class);
                return ipAddressDto;
            }else{
                return null;
            }

        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return null;
        }
    }


}
